package com.angel.dtoutlis;

import com.angel.bean.Branches;
import com.angel.bean.Employee;
import com.angel.dto.BranchesDto;
import com.angel.service.BranchesService;
import com.angel.service.EmployeeService;

import java.util.ArrayList;
import java.util.List;

public class BranchesDtoUtil {

    /**
     * 将分店和父店进行dto封装
     *
     * @param branchesList 分店集合
     * @return dto集合
     */
    public static List<BranchesDto> getBranchesDto(List<Branches> branchesList, BranchesService branchesService, EmployeeService employeeService) {
        if (isNull(branchesList)) {
            return null;
        }
        List<BranchesDto> branchesDtoList = new ArrayList<>();
        //遍历查询出来的父店
        for (int i = 0; i < branchesList.size(); i++) {
            // 获得父店，并封装
            Integer brid = branchesList.get(i).getUpperId();
            Branches branches = new Branches();
            branches.setState(1);
            branches.setBranId(brid);
            // 获得员工，并封装
            Integer epid = branchesList.get(i).getBranPrinciple();
            Employee employee = new Employee();
            employee.setEmId(epid);
            employee.setState(1);
            // 查找负责人
            employee = employeeService.findOne(employee);
            // 查找父店
            branches = branchesService.findOneBranches(branches);

            if (employee != null && branches != null) {
                branchesDtoList.add(new BranchesDto(branches.getBranName(), branchesList.get(i), employee));
            }
        }
        return branchesDtoList;
    }


    //父店，分店，员工类型为店长
   /* public static List<BranchesDto> getBranchesDto(List<Branches> branchesList, List<Employee> employeeList) {
        if (isNull(branchesList) || isNull(employeeList)) {
            return null;
        }
        List<BranchesDto> branchesDtoList = new ArrayList<>();
        //遍历查询出来的父店
        for (int i = 0; i < branchesList.size(); i++) {
            for (int j = 0; j < branchesList.size(); j++) {
                for (int x = 0; x < employeeList.size(); x++) {
                    //如果父店id等于分店id
                    if ((branchesList.get(i).getUpperId().equals(branchesList.get(j).getBranId()) &&
                            branchesList.get(i).getBranPrinciple().equals(employeeList.get(x).getEmId()))) {
                        branchesDtoList.add(new BranchesDto(branchesList.get(j).getBranName(), branchesList.get(i), employeeList.get(x)));
                        break;
                    }
                }
            }
        }
        return branchesDtoList;
    }
*/

    /**
     * 判断传入集合是否是空值
     *
     * @param list
     * @return
     */
    private static boolean isNull(List list) {
        if (list == null || list.isEmpty()) {
            return true;
        }
        return false;
    }
}
